package com.dirk.happie.activities;

import java.util.List;

import android.app.Activity;
import android.app.Dialog;
import android.content.Intent;
import android.os.Bundle;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.Window;
import android.widget.Button;
import android.widget.TextView;
import android.widget.Toast;

import com.dirk.happie.R;
import com.dirk.happie.beans.CompanyBean;
import com.dirk.happie.beans.FavoritesBean;
import com.dirk.happie.network.GetCompaniesTask;
import com.dirk.happie.network.GetFavoritesTask;
import com.dirk.happie.network.RequestCompleteListener;
import com.dirk.happie.utils.App;
import com.dirk.happie.utils.Availablity;
import com.dirk.happie.utils.Common;

public class LoadingActivity extends Activity {

	private GetFavoritesTask getFavoritesTask = null;
	private GetCompaniesTask getCompaniesTask = null;
	private App mApp;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_loading);
		this.mApp = ((App) getApplication());
//		mApp.setHappie_prefs(getSharedPreferences(getString(R.string.pref_file_name), MODE_PRIVATE));
//		mApp.setDefFavoriteId(mApp.getHappie_prefs().getString(Keys.KEY_DEF_FAVORITE_ID, ""));
//		if(mApp.getDefFavoriteId().equals("")){
//			Toast.makeText(this, getString(R.string.def_favorite_not_exits), Toast.LENGTH_SHORT).show();
//		} else {
//			Toast.makeText(this, getString(R.string.def_favorite_exists), Toast.LENGTH_SHORT).show();
//		}
//		mApp.setLoggedIn(mApp.getHappie_prefs().getBoolean(Keys.KEY_USER_LOGGED_IN, false));
//		if (mApp.isLoggedIn()) {
//			mApp.setUserId(mApp.getHappie_prefs().getString(Keys.KEY_USER_ID, ""));
//		}

		getFavoritesTask = new GetFavoritesTask(this);
		getCompaniesTask = new GetCompaniesTask(this);

		boolean connected = Availablity.isNetworkAvailable(this);

		if (connected) {
			getCompanies();

		} else {
			showMessageDialog(getString(R.string.no_connection));
		}
	}

	private void getCompanies() {

		getCompaniesTask.setRequestCompleteListener(new RequestCompleteListener<List<CompanyBean>>() {

			@Override
			public void onSuccess(List<CompanyBean> result) {
				mApp.setCompanies(result);
				mApp.getHappie_prefs().edit().putString("Companies", Common.companyArrayToJson(mApp.getCompanies())).commit();
				// companies = mApp.getCompanies();
				// mApp.setCompanyId(companies.get(mApp.position).getCompanyId());
				// companyCount = companies.size();
				if (result != null) {
					mApp.addDefaultFavorite();
//					for (CompanyBean companyBean : mApp.getCompanies()) {
//						if (mApp.getDefFavoriteId().equals(companyBean.getCompanyId())) {
////							mApp.getFavorites().add(companyBean);
//							Common.addFavoriteIfExists(mApp.getFavorites(), companyBean);
//						}
//					}
					getFavorites();
				} else {
					showMessageDialog(getString(R.string.no_connection));
				}

			}

			@Override
			public void onFailure(final String message) {
				runOnUiThread(new Runnable() {

					@Override
					public void run() {
						Toast.makeText(getApplicationContext(), message, Toast.LENGTH_SHORT).show();
					}
				});
			}
		});

		getCompaniesTask.execute();
	}

	private void getFavorites() {
		getFavoritesTask.setRequestCompleteListener(new RequestCompleteListener<List<FavoritesBean>>() {

			@Override
			public void onSuccess(List<FavoritesBean> result) {
				for (FavoritesBean favoritesBean : result) {
					for (CompanyBean companyBean : mApp.getCompanies()) {
						if (favoritesBean.getCompanyId().equals(companyBean.getCompanyId())) {
//							mApp.getFavorites().add(companyBean);
							Common.addFavoriteIfExists(mApp.getFavorites(), companyBean);
						}
					}
				}
				mApp.getHappie_prefs().edit().putString("Favorites", Common.companyArrayToJson(mApp.getFavorites())).commit();
				Intent intent;
				intent = new Intent(LoadingActivity.this, MenuActivity.class);
				startActivity(intent);
				finish();
			}

			@Override
			public void onFailure(final String message) {
				runOnUiThread(new Runnable() {

					@Override
					public void run() {
						Toast.makeText(getApplicationContext(), message, Toast.LENGTH_SHORT).show();
						Intent intent;
						intent = new Intent(LoadingActivity.this, MenuActivity.class);
						startActivity(intent);
						finish();
					}
				});
			}
		});

		if (mApp.isLoggedIn()) {
			getFavoritesTask.execute(mApp.getUserId());
		} else {
			Toast.makeText(getApplicationContext(), getString(R.string.not_logged_in), Toast.LENGTH_SHORT).show();
			Intent intent;
			intent = new Intent(LoadingActivity.this, MenuActivity.class);
			startActivity(intent);
			finish();
		}

	}

	public Dialog alertDialog;

	public void showMessageDialog(String message) {
		if (alertDialog != null && alertDialog.isShowing())
			return;

		alertDialog = new Dialog(this);
		alertDialog.requestWindowFeature(Window.FEATURE_NO_TITLE);
		alertDialog.setContentView(R.layout.dialog);
		alertDialog.setCancelable(true);

		TextView text = (TextView) alertDialog.findViewById(R.id.dialog_title);
		text.setTextSize(18);
		text.setText(message);

		Button okButton = (Button) alertDialog.findViewById(R.id.ok_button);
		okButton.setText(R.string.ok_error);
		okButton.setOnClickListener(new OnClickListener() {

			public void onClick(View v) {
				alertDialog.cancel();
				finish();
			}
		});
		alertDialog.show();
	}

	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		// Inflate the menu; this adds items to the action bar if it is present.
		getMenuInflater().inflate(R.menu.activity_loading, menu);
		return true;
	}

	@Override
	protected void onDestroy() {
		if (getFavoritesTask != null) {
			getFavoritesTask.setRequestCompleteListener(null);
		}
		if (getCompaniesTask != null) {
			getCompaniesTask.setRequestCompleteListener(null);
		}
		super.onDestroy();
	}

}
